package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

import logic.CRUDUsers;

public class AddUser extends JFrame {

	private JPanel contentPane;
	private final JLabel lblNome = new JLabel("Nome: ");
	private JTextField nomeField;
	private JTextField sobreNomeField;
	private JTextField salaField;
	private JTextField emailField;
	private JPasswordField senhaField;
	private JPasswordField rptSenhaField;


	/**
	 * Create the frame.
	 */
	public AddUser() {
		setTitle("Coolrent - Cadastrar novo usuário.");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 345, 319);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		lblNome.setBounds(10, 11, 99, 28);
		contentPane.add(lblNome);
		
		JLabel lblSobrenome = new JLabel("Sobrenome");
		lblSobrenome.setBounds(10, 54, 83, 28);
		contentPane.add(lblSobrenome);
		
		JLabel lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(10, 126, 83, 28);
		contentPane.add(lblEmail);
		
		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setBounds(10, 169, 83, 28);
		contentPane.add(lblSenha);
		
		JLabel lblRepitaASenha = new JLabel("Repita a senha:");
		lblRepitaASenha.setBounds(10, 214, 107, 28);
		contentPane.add(lblRepitaASenha);
		
		JLabel lblSala = new JLabel("Sala:");
		lblSala.setBounds(10, 97, 46, 14);
		contentPane.add(lblSala);
		
		nomeField = new JTextField();
		nomeField.setBounds(105, 13, 212, 24);
		contentPane.add(nomeField);
		nomeField.setColumns(10);
		
		sobreNomeField = new JTextField();
		sobreNomeField.setColumns(10);
		sobreNomeField.setBounds(105, 56, 212, 24);
		contentPane.add(sobreNomeField);
		
		salaField = new JTextField();
		salaField.setColumns(10);
		salaField.setBounds(105, 92, 212, 24);
		contentPane.add(salaField);
		
		emailField = new JTextField();
		emailField.setColumns(10);
		emailField.setBounds(105, 128, 212, 24);
		contentPane.add(emailField);
		
		senhaField = new JPasswordField();
		senhaField.setColumns(10);
		senhaField.setBounds(105, 171, 212, 24);
		contentPane.add(senhaField);
		
		rptSenhaField = new JPasswordField();
		rptSenhaField.setColumns(10);
		rptSenhaField.setBounds(105, 216, 212, 24);
		contentPane.add(rptSenhaField);
		
		JButton btnCadastrar = new JButton("Cadastrar");

		btnCadastrar.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (!Arrays.equals(senhaField.getPassword(), rptSenhaField.getPassword())){
					JOptionPane.showMessageDialog(null, "senhas não conferem.");
				} else {
					try {
						CRUDUsers.addUser(
								nomeField.getText(), 
								sobreNomeField.getText(), 
								emailField.getText(), 
								new String(senhaField.getPassword()) , 
								salaField.getText());
						        JOptionPane.showMessageDialog(null, "Usuario cadastrado");
					} catch (Exception e) {
						JOptionPane.showMessageDialog(null, e.getMessage());
					}
				}
			}
		});
		btnCadastrar.setBounds(115, 247, 90, 28);
		contentPane.add(btnCadastrar);
		
		
	}
}
